//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.pc_rfid.api;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.util.Log;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

public class PC_USB extends PC_COMM {
    private static final String TAG = "PC_USB";
    private static final String ACTION_USB_PERMISSION = "com.pc_rfid.api.USB_PERMISSION";
    private static int VID = 65534;
    private static int PID = 145;
    private static Context context = null;
    private UsbDevice usb_dev = null;
    private UsbManager usb_manager = null;
    private UsbDeviceConnection usb_conn = null;
    private UsbInterface usb_Interface = null;
    private UsbEndpoint epOut = null;
    private UsbEndpoint epIn = null;
    private UsbEndpoint epIn_initiative = null;
    private UsbEndpoint epControl = null;
    private static boolean isHavePermission = false;
    private PendingIntent intent = null;
    private byte[] recv_buffer = null;
    private final int BUFFER_SIZE = 1024;
    private int recv_data_Len = 0;
    private int recv_data_pos = 0;
    private final Integer buffer_sock = new Integer(0);
    private static USBReceiver usb_Receiver = null;
    private static boolean isInit = false;
    private static OnUsbListener mOnUsbListener = null;

    public static UsbDevice[] scanUSB(int vendorId, int productId) {
        if (context == null) {
            return null;
        } else {
            UsbManager usb_mg = (UsbManager)context.getApplicationContext().getSystemService("usb");
            if (usb_mg == null) {
                return null;
            } else {
                Vector<UsbDevice> list_dev = new Vector();
                Map<String, UsbDevice> mapdev = usb_mg.getDeviceList();
                Iterator var5 = mapdev.values().iterator();

                while(var5.hasNext()) {
                    UsbDevice device = (UsbDevice)var5.next();
                    if (device.getVendorId() == vendorId && device.getProductId() == productId) {
                        list_dev.add(device);
                    }
                }

                if (list_dev.size() > 0) {
                    return (UsbDevice[])list_dev.toArray(new UsbDevice[list_dev.size()]);
                } else {
                    return null;
                }
            }
        }
    }

    private boolean connection(UsbDevice dev) {
        this.usb_dev = dev;
        if (this.usb_dev == null) {
            return false;
        } else {
            this.usb_manager = (UsbManager)context.getApplicationContext().getSystemService("usb");
            this.usb_Interface = this.usb_dev.getInterface(0);

            for(int i = 0; i < this.usb_Interface.getEndpointCount(); ++i) {
                UsbEndpoint ep = this.usb_Interface.getEndpoint(i);
                switch(ep.getType()) {
                    case 0:
                        this.epControl = ep;
                    case 1:
                    default:
                        break;
                    case 2:
                        if (0 == ep.getDirection()) {
                            this.epOut = ep;
                        } else {
                            this.epIn = ep;
                        }
                        break;
                    case 3:
                        if (ep.getDirection() == 0) {
                            this.epOut = ep;
                        }

                        if (ep.getDirection() == 128) {
                            this.epIn = ep;
                        }
                }
            }

            isHavePermission = this.usb_manager.hasPermission(this.usb_dev);
            if (isHavePermission) {
                this.usb_conn = this.usb_manager.openDevice(this.usb_dev);
            } else {
                this.usb_manager.requestPermission(this.usb_dev, this.intent);
                if (!this.usb_manager.hasPermission(this.usb_dev)) {
                    Log.e("PC_USB", "没有权限");
                    return false;
                }

                this.usb_conn = this.usb_manager.openDevice(this.usb_dev);
            }

            if (this.usb_conn == null) {
                Log.e("PC_USB", "不能连接到设备");
                return false;
            } else if (this.usb_conn.claimInterface(this.usb_Interface, true) && this.usb_conn != null) {
                return true;
            } else {
                Log.e("PC_USB", "无法打开连接通道。");
                this.usb_conn.close();
                return false;
            }
        }
    }

    public int open(UsbDevice dev) {
        if (!isInit) {
            return -1;
        } else {
            this.intent = PendingIntent.getBroadcast(context, 0, new Intent("com.pc_rfid.api.USB_PERMISSION"), 0);
            if (!this.connection(dev)) {
                return -1;
            } else {
                if (!this.isOpen) {
                    this.isOpen = true;
                    if (this.isOpen && this.usb_conn != null && this.epIn != null) {
                        byte[] buffer = new byte[256];

                        for(int ret = 1; ret > 0; ret = this.usb_conn.bulkTransfer(this.epIn, buffer, 256, 500)) {
                        }
                    }
                }

                return 0;
            }
        }
    }

    public PC_USB(UsbDevice dev) {
        if (dev != null) {
            if (dev.getVendorId() != 65534 || dev.getProductId() != 145 && dev.getProductId() != 146) {
                return;
            }

            this.open(dev);
        }

    }

    public int Send(byte[] data) {
        if (this.isOpen && this.usb_conn != null && this.epOut != null) {
            this.clearBuffer();
            int len = (data[1] & 255) + 1;
            byte[] buffer = new byte[len + 1];
            buffer[0] = (byte)len;

            for(int i = 0; i < len; ++i) {
                buffer[i + 1] = data[i];
            }

            return this.usb_conn.bulkTransfer(this.epOut, buffer, buffer.length, 1000);
        } else {
            return -1;
        }
    }

    public int Receive(byte[] data) {
        if (this.isOpen && this.usb_conn != null && this.epIn != null) {
            byte[] buffer = new byte[256];

            long time_1;
            for(long time_2 = time_1 = System.currentTimeMillis(); time_2 - time_1 < 2000L; time_2 = System.currentTimeMillis()) {
                int ret = this.usb_conn.bulkTransfer(this.epIn, buffer, 256, 100);
                int real_len = ret - 1;
                byte[] real_data = null;
                int i;
                if (ret > 0) {
                    real_data = new byte[real_len];

                    for(i = 0; i < real_len; ++i) {
                        real_data[i] = buffer[i + 1];
                    }
                }

                if (this.ParsePack(real_data, real_len, data)) {
                    i = (data[1] & 255) + 1;
                    return i;
                }
            }

            return 0;
        } else {
            return -1;
        }
    }

    public int close() {
        if (this.usb_conn != null) {
            this.usb_conn.close();
            this.usb_conn.releaseInterface(this.usb_Interface);
            this.usb_conn = null;
        }

        this.usb_dev = null;
        this.isOpen = false;
        return 0;
    }

    public void clearBuffer() {
        this.clearParse();
    }

    public int Send_Receive(RFdata data) {
        if (!this.isOpen) {
            return -1;
        } else {
            int len = this.Send(data.SendData);
            if (len >= 0) {
                len = this.Receive(data.RecvData);
            }

            return len;
        }
    }

    public static void init(Context ctx) {
        if (ctx != null) {
            isInit = true;
            context = ctx;
            usb_Receiver = new USBReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction("com.pc_rfid.api.USB_PERMISSION");
            filter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
            filter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
            context.registerReceiver(usb_Receiver, filter);
        }
    }

    public static void exit() {
        if (isInit) {
            if (context != null && usb_Receiver != null) {
                context.unregisterReceiver(usb_Receiver);
                usb_Receiver = null;
            }

            isInit = false;
            context = null;
            mOnUsbListener = null;
        }

    }

    public static void setOnUsbListener(OnUsbListener Listener) {
        mOnUsbListener = Listener;
    }

    public static class USBReceiver extends BroadcastReceiver {
        public USBReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            UsbDevice device = (UsbDevice)intent.getParcelableExtra("device");
            if ("com.pc_rfid.api.USB_PERMISSION".equals(action)) {
                PC_USB.isHavePermission = true;
            }

            if (action.equals("android.hardware.usb.action.USB_DEVICE_ATTACHED") && device.getVendorId() == 65534 && (device.getProductId() == 145 || device.getProductId() == 146) && PC_USB.mOnUsbListener != null) {
                PC_USB.mOnUsbListener.onStateChanged(device, true);
            }

            if (action.equals("android.hardware.usb.action.USB_DEVICE_DETACHED") && device.getVendorId() == 65534 && (device.getProductId() == 145 || device.getProductId() == 146) && PC_USB.mOnUsbListener != null) {
                PC_USB.mOnUsbListener.onStateChanged(device, false);
            }

        }
    }
}
